package org.itboys.kitchen.service.mysql.order;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.kitchen.dao.order.KitchenOrderMapper;
import org.itboys.kitchen.dao.order.KitchenOrderProductMapper;
import org.itboys.kitchen.entity.mysql.order.KitchenOrder;
import org.itboys.kitchen.entity.mysql.order.KitchenOrderProduct;
import org.itboys.mongodb.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;


/**
 * 作者：jiangxiong
 * 日期：2016年11月25日
 * 联系方式 ：empty90911@126.com
 * 描述：农厨_订单_核销_后台_service
 */
@Service
public class KitchenOrderAccountService {
	@Autowired
	private KitchenOrderMapper orderMapper;
	@Autowired
	private KitchenOrderProductMapper orderProductMapper;
	/***-----------------采购订单核销中心--**/

	public PageResult<KitchenOrder> selectOrder(HttpServletRequest request){
		Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"orderNo","memberId","isAccount","userType");
		//只查询采购订单
		param.put("type",3);
		//只查询已经配送的以后的
		List<Integer> orderStatuses=new ArrayList<Integer>();
		orderStatuses.add(3);
		orderStatuses.add(4);
		orderStatuses.add(5);
		param.put("orderStatuses",orderStatuses);
		Serializable count=orderMapper.count(param);
		return  new PageResult<KitchenOrder>(orderMapper.select(param),count==null?0L:Long.valueOf(count.toString()));
	}

	/**
     *(不支持批量核销)
	 * @param orderIds
     * @param accountRemark
	 */
	/**
	 *  核销采购订单
	 * @param orderId
	 * @param accountRemark
	 * @param accountAmount
	public void account(Long orderId, BigDecimal accountAmount, String accountRemark){
		KitchenOrder order = new KitchenOrder();
		order.setId(orderId);
		order.setIsAccount(1);
		order.setAccountRemark(accountRemark);
		order.setAccountAmount(accountAmount);
		orderMapper.update(order);
		*//*if(ListUtil.isNull(orderIds))return;
		Map<String,Object> param =new HashMap<String,Object>();
		param.put("ids",orderIds);
		orderMapper.account(orderIds,accountRemark);*//*

	}*/
	/**
	 *
	 * @param orderId
	 * @param accountRemark
	 * @param accountAmount
	 */
	/**
	 *
	 * @param orderId
	 * @param orderProductIds
	 * @param accountAmounts
	 * @param accountRemark
	 */
	/**
	 * 核销采购订单。按订单商品实际核销订单金额
	 * @param orderId
	 * @param orderProductIds
	 * @param accountPrices 单品实际核销价格集合
	 * @param accountNums 单品实际核销数量集合
	 * @param accountRemark
	 */
	@Transactional
	public void account(Long orderId,List<Long> orderProductIds,List<BigDecimal> accountPrices,List<Integer> accountNums, String accountRemark){
		Double accountAmount=0.00;
		for(int i=0;i<orderProductIds.size();i++){
			Long orderProductId=orderProductIds.get(i);
			KitchenOrderProduct orderProduct=new KitchenOrderProduct();
			orderProduct.setId(orderProductId);
			orderProduct.setAccountPrice(accountPrices.get(i));
			orderProduct.setAccountNum(accountNums.get(i));
			accountAmount+=accountPrices.get(i).doubleValue();
			orderProductMapper.update(orderProduct);

		}
		KitchenOrder order = new KitchenOrder();
		order.setId(orderId);
		order.setIsAccount(1);
		order.setAccountRemark(accountRemark);
		order.setAccountAmount(new BigDecimal(accountAmount));
		orderMapper.update(order);

	}
}
